using Microsoft.AspNetCore.Mvc;
using ZR.Model.ProdManage.Dto;
using ZR.Model.ProdManage;
using ZR.Service.ProdManage.IProdManageService;
using ZR.Admin.WebApi.Filters;
using MiniExcelLibs;

//创建时间：2025-05-18
namespace ZR.Admin.WebApi.Controllers.ProdManage
{
    /// <summary>
    /// 二级包装
    /// </summary>
    [Verify]
    [Route("ProdManage/ProdTwoPack")]
    public class ProdTwoPackController : BaseController
    {
        /// <summary>
        /// 二级包装接口
        /// </summary>
        private readonly IProdTwoPackService _ProdTwoPackService;

        public ProdTwoPackController(IProdTwoPackService ProdTwoPackService)
        {
            _ProdTwoPackService = ProdTwoPackService;
        }

        /// <summary>
        /// 查询二级包装列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("pageList")]
        [ActionPermissionFilter(Permission = "prod:twoPack:list")]
        public IActionResult PageProdTwoPack([FromQuery] ProdTwoPackQueryDto parm)
        {
            var response = _ProdTwoPackService.GetPageList(parm);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询二级包装详情
        /// </summary>
        /// <param name="PackId"></param>
        /// <returns></returns>
        [HttpGet("{PackId}")]
        [ActionPermissionFilter(Permission = "prod:twoPack:query")]
        public IActionResult GetProdTwoPack(string PackId)
        {
            var response = _ProdTwoPackService.GetInfo(PackId);
            
            var info = response.Adapt<ProdTwoPackDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加二级包装
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "prod:twoPack:add")]
        [Log(Title = "二级包装", BusinessType = BusinessType.INSERT)]
        public IActionResult AddProdTwoPack([FromBody] ProdTwoPackDto parm)
        {
            var modal = parm.Adapt<ProdTwoPack>().ToCreate(HttpContext);

            var response = _ProdTwoPackService.AddProdTwoPack(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 更新二级包装
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "prod:twoPack:edit")]
        [Log(Title = "二级包装", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateProdTwoPack([FromBody] ProdTwoPackDto parm)
        {
            var modal = parm.Adapt<ProdTwoPack>().ToUpdate(HttpContext);
            var response = _ProdTwoPackService.UpdateProdTwoPack(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除二级包装
        /// </summary>
        /// <returns></returns>
        [HttpPost("delete")]
        [ActionPermissionFilter(Permission = "prod:twoPack:delete")]
        [Log(Title = "二级包装", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteProdTwoPack([FromBody] string[] ids)
        {
            return ToResponse(_ProdTwoPackService.Delete(ids));
        }

        /// <summary>
        /// 导出二级包装
        /// </summary>
        /// <returns></returns>
        [Log(Title = "二级包装", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        [HttpGet("export")]
        [ActionPermissionFilter(Permission = "prod:twoPack:export")]
        public IActionResult Export([FromQuery] ProdTwoPackQueryDto parm)
        {
            parm.PageNum = 1;
            parm.PageSize = 100000;
            var list = _ProdTwoPackService.ExportList(parm).Result;
            if (list == null || list.Count <= 0)
            {
                return ToResponse(ResultCode.FAIL, "没有要导出的数据");
            }
            var result = ExportExcelMini(list, "二级包装", "二级包装");
            return ExportExcel(result.Item2, result.Item1);
        }

        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="formFile"></param>
        /// <returns></returns>
        [HttpPost("importData")]
        [Log(Title = "二级包装导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false)]
        [ActionPermissionFilter(Permission = "prod:twoPack:import")]
        public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile)
        {
            List<ProdTwoPackDto> list = new();
            using (var stream = formFile.OpenReadStream())
            {
                list = stream.Query<ProdTwoPackDto>(startCell: "A1").ToList();
            }

            return SUCCESS(_ProdTwoPackService.ImportProdTwoPack(list.Adapt<List<ProdTwoPack>>()));
        }

        /// <summary>
        /// 二级包装导入模板下载
        /// </summary>
        /// <returns></returns>
        [HttpGet("importTemplate")]
        [Log(Title = "二级包装模板", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        [AllowAnonymous]
        public IActionResult ImportTemplateExcel()
        {
            var result = DownloadImportTemplate(new List<ProdTwoPackDto>() { }, "ProdTwoPack");
            return ExportExcel(result.Item2, result.Item1);
        }

    }
}